package com.it.myd.controller.main.module;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.it.myd.bean.box.UNOCard;
import com.it.myd.bean.main.ReturnValue;
import com.it.myd.bean.main.User;
import com.it.myd.tool.UnoTool;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/module/uno")
public class UnoController {

    @Autowired
    private UnoTool unoTool;

    private String isStart = "0";

    @RequestMapping("getUno")
    @ResponseBody
    public ReturnValue getUno(HttpServletRequest request){
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession();
        String count = request.getParameter("count");
        String first = request.getParameter("first");
        User user = (User) session.getAttribute("user");
        List<User> playlist = (List<User>) application.getAttribute("playlist");
        application.setAttribute("playlist",playlist);
        ReturnValue<UNOCard> ret = new ReturnValue();
        ret.isSuccess();
        List<UNOCard> listuno = new ArrayList<>();
        ret.setRetList(unoTool.getUnoCard(Integer.parseInt(count)));
        return ret;
    }

    @RequestMapping("mp")
    @ResponseBody
    public ReturnValue mp(HttpServletRequest request){
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        List<User> playlist = (List<User>) application.getAttribute("playlist");
        int unojf = (int) application.getAttribute("unojf");
        unojf += 1;
        application.setAttribute("unojf",unojf);
        int addtwo = (int) application.getAttribute("addtwo");
        for (User u : playlist) {
            if (u.getUser_id()== user.getUser_id()) {
                String ncc = String.valueOf(Integer.parseInt(u.getCard_count()) + 1 + addtwo);
                u.setCard_count(ncc);
                break;
            }
        }
        application.setAttribute("addtwo",0);
        for (int i = 0; i < playlist.size(); i++) {
            User u = (User) playlist.get(i);
            if (u.getUser_id()== user.getUser_id()) {
                u.setCard_content("摸牌");
                u.setIsplay("0");
                if(i + 1 == playlist.size()){
                    for (int j = 0; j < playlist.size(); j++) {
                        User u1 = (User) playlist.get(j);
                        if(u1.getPlay_state().equals("1")){
                            playlist.get(j).setIsplay("1");
                            break;
                        }
                    }
                }else{
                    boolean flag = true;
                    for (int j = i+1; j < playlist.size(); j++) {
                        User u1 = (User) playlist.get(j);
                        if(u1.getPlay_state().equals("1")){
                            playlist.get(j).setIsplay("1");
                            flag = false;
                            break;
                        }
                    }
                    if (flag) {
                        for (int j = 0; j < playlist.size(); j++) {
                            User u1 = (User) playlist.get(j);
                            if(u1.getPlay_state().equals("1")){
                                playlist.get(j).setIsplay("1");
                                break;
                            }
                        }
                    }
                }
                application.setAttribute("playlist",playlist);
                break;
            }
        }
        ReturnValue<UNOCard> ret = new ReturnValue();
        ret.isSuccess();
        return ret;
    }

    @RequestMapping("getPlay")
    @ResponseBody
    public ReturnValue getPlay(HttpServletRequest request){
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        List<User> playlist = (List<User>) application.getAttribute("playlist");
        ReturnValue<User> ret = new ReturnValue();
        ret.setRetList(playlist);
        ret.isSuccess();
        ret.setRetDesc(String.valueOf((int) application.getAttribute("addtwo")));
        return ret;
    }

    @RequestMapping("getrule")
    @ResponseBody
    public ReturnValue getrule(HttpServletRequest request){
        ServletContext application = request.getServletContext();
        ReturnValue<UNOCard> ret = new ReturnValue();
        UNOCard ruleuno = (UNOCard) application.getAttribute("ruleuno");
        if(ruleuno==null){
            ret.isFail();
            return ret;
        }
        List<UNOCard> list = new ArrayList<>();
        list.add(ruleuno);
        ret.isSuccess();
        ret.setRetList(list);
        return ret;
    }

    @RequestMapping("ready")
    @ResponseBody
    public ReturnValue ready(HttpServletRequest request){
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        List<User> playlist = (List<User>) application.getAttribute("playlist");
        boolean flag = true;
        for (User u : playlist) {
            if (u.getUser_id()== user.getUser_id()) {
                u.unoPlayInit();
                u.setPlay_state("1");
            }
            if(u.getPlay_state().equals("0")){
                flag = false;
            }
        }
        if(flag){
            isStart(request);
            isStart = "1";
        }
        application.setAttribute("playlist",playlist);
        ReturnValue<User> ret = new ReturnValue();
        ret.isSuccess();
        return ret;
    }

    public void isStart(HttpServletRequest request){
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession();
        List<User> playlist = (List<User>) application.getAttribute("playlist");
        Random random = new Random();
        int start = random.nextInt(playlist.size());
        for (int i = 0; i < playlist.size(); i++) {
            User u = (User) playlist.get(i);
            u.unoPlayStart();
            if (i == start) {
                u.setIsplay("1");
            }
        }
        isStart = "0";
        application.setAttribute("playlist",playlist);
        application.setAttribute("addtwo",0);
        application.setAttribute("ruleuno",null);
        application.setAttribute("unojf",1);
    }

    public void isover(HttpServletRequest request){
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        List<User> playlist = (List<User>) application.getAttribute("playlist");
        for (int i = 0; i < playlist.size(); i++) {
            User u = (User) playlist.get(i);
            u.setPlay_state("0");
            if (u.getUser_id()== user.getUser_id()) {
                int unojf = (int) application.getAttribute("unojf");
                u.setGame_count(String.valueOf(Integer.parseInt(u.getGame_count())+unojf));
            }
        }
        isStart = "0";
        application.setAttribute("playlist",playlist);
        application.setAttribute("ruleuno",null);
        application.setAttribute("unojf",1);
        application.setAttribute("addtwo",0);
    }

    @RequestMapping("reload")
    @ResponseBody
    public void reload(HttpServletRequest request){
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession();
        List<User> playlist = (List<User>) application.getAttribute("playlist");
        playlist.clear();
        isStart = "0";
        application.setAttribute("playlist",playlist);
        application.setAttribute("ruleuno",null);
        application.setAttribute("unojf",1);
    }


    @RequestMapping("noready")
    @ResponseBody
    public ReturnValue noReady(HttpServletRequest request){
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        List<User> playlist = (List<User>) application.getAttribute("playlist");
        boolean flag = true;
        for (User u : playlist) {
            if (u.getUser_id()== user.getUser_id()) {
                u.setPlay_state("0");
                application.setAttribute("playlist",playlist);
                break;
            }
        }
        ReturnValue<User> ret = new ReturnValue();
        ret.isSuccess();
        return ret;
    }

    @RequestMapping("isstart")
    @ResponseBody
    public ReturnValue isstart(HttpServletRequest request){
        ReturnValue<User> ret = new ReturnValue();
        if(isStart.equals("1")){
            ret.isSuccess();
        }else{
            ret.isFail();
        }
        return ret;
    }

    @RequestMapping("sendcard")
    @ResponseBody
    public ReturnValue sendCard(HttpServletRequest request){
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        List<User> playlist = (List<User>) application.getAttribute("playlist");
        String unojson = request.getParameter("json");
        String color = request.getParameter("color");
        ObjectMapper objectMapper = new ObjectMapper();
        UNOCard uno = new UNOCard();
        try {
            uno = objectMapper.readValue(unojson, UNOCard.class);
            if(!color.equals("")){
                uno.setC_color1(color);
                String lx = "";
                if(color.equals("#000000")){lx = "黑月";uno.setC_icon1("&#xe6c2;");}
                else if(color.equals("#ff0000")){lx = "红日";uno.setC_icon1("&#xe748;");}
                else if(color.equals("#ffff00")){lx = "黄星";uno.setC_icon1("&#xe67b;");}
                else if(color.equals("#0000ff")){lx = "蓝雪";uno.setC_icon1("&#xe6b1;");}
                uno.setC_fields(uno.getC_fields()+","+color);
                uno.setC_content(uno.getC_content()+" - "+lx);
            }
            application.setAttribute("ruleuno",uno);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        if(uno.getC_type().equals("addtwoUno") || uno.getC_type().equals("alloutaddtwoUno")){
            int addtwo = (int) application.getAttribute("addtwo");
            application.setAttribute("addtwo",addtwo+2);
        }else if(uno.getC_type().equals("reversalUno")){
            Collections.reverse(playlist);
        }else if(uno.getC_type().equals("alloutaddfourUno")){
            int addtwo = (int) application.getAttribute("addtwo");
            application.setAttribute("addtwo",addtwo+4);
        }

        int cps =  1;//出牌数
        int tgs =  0;//跳过玩家数

        if(uno.getC_type().equals("stopUno")){
            tgs = 1;
        }

        if(uno.getC_type().equals("switchUno")){
            for (int i = 0; i < playlist.size(); i++) {
                User u1 = (User) playlist.get(i);
                if(u1.getPlay_state().equals("1")){

                }
            }
        }

        for (int i = 0; i < playlist.size(); i++) {
            User u = (User) playlist.get(i);
            if (u.getUser_id()== user.getUser_id()) {//找到出牌玩家位置
                int a = Integer.parseInt(u.getCard_count()) - cps;//减少牌数
                String ncc = String.valueOf(a);
                u.setCard_count(ncc);
                u.setCard_content(uno.getC_content());
                u.setIsplay("0");//设置不可出牌
                boolean f = true;
                for (int j = i + 1; j < playlist.size(); j++) {
                    User u1 = (User) playlist.get(j);
                    if(u1.getPlay_state().equals("1")){//找到状态为参与者的人
                        if(tgs == 0){
                            playlist.get(j).setIsplay("1");//给与出牌能力
                            f = false;
                            break;
                        }else{
                            tgs = tgs - 1;
                        }
                    }
                }
                while(f){
                    for (int j = 0; j < playlist.size(); j++) {//从队列头循环
                        User u1 = (User) playlist.get(j);
                        if(u1.getPlay_state().equals("1")){//找到状态为参与者的人
                            if(tgs == 0){
                                playlist.get(j).setIsplay("1");//给与出牌能力
                                f = false;
                                break;
                            }else{
                                tgs = tgs - 1;
                            }
                        }
                    }
                }
                if(a == 0){//出完牌结束
                    isover(request);
                }
                application.setAttribute("playlist",playlist);
                break;
            }
        }
        ReturnValue<User> ret = new ReturnValue();
        ret.setRetList(playlist);
        ret.isSuccess();
        return ret;
    }
}
